﻿Public Class DeleteInvestor

#Region "Declaration"
    Private stopresize As Boolean = False
    Private _FundId As String
    Public Event deleteInvestor(ByVal dt As DataTable, ByVal isDeleted As Boolean)
    Private _datatable As DataTable
    Delegate Sub deleteinv()
    Delegate Sub refreshscreen(ByVal dt As DataTable, ByVal isDeleted As Boolean)

#End Region

#Region "Properties"
    Public Property Datatable() As DataTable
        Get
            Return _datatable
        End Get
        Set(ByVal value As DataTable)
            _datatable = value
        End Set
    End Property
#End Region

#Region "Method and Events"

    Public Sub New(ByVal dt As DataTable)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        Datatable = dt
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub yes()
        lblmessage.Text = ""
        'Dim CAddModifyInvestor As New BusinessLayer.cAddModifyInvestor
        Dim _strCurrentInvestors As String = ""
        Dim _strCurrentInvestorsName As String = ""
        Dim _strHistoryInvestorsName As String = ""
        Dim _strHistoryInvestors As String = ""
        Dim _strLockedInvestorNames As String = ""
        Dim _strInvestorMessage As String = ""
        Dim result As String = ""
        Dim message As String = ""
        Dim strClients As String = ""
        Dim strClient As String = ""
        Dim _strCurrentInvDt As DataTable = Nothing
        Dim _strLockedInvDt As DataTable = Nothing
        Dim _strTempDt As DataTable = Nothing
        Dim _strHistoryInvDt As DataTable = Nothing
        Dim _strDeletedRows As DataTable = Nothing
        Dim _strreference As DataTable = Nothing
        Dim objInv As cAddModifyInvestor = Nothing
        Dim temp As DataTable = Nothing
        Dim id As String = ""
        Dim IsDeleted As Boolean = False
        Dim ref As refreshscreen = Nothing
        Try
            lblInform.Visible = False
            temp = CType(grdInvestors.DataSource, DataTable)
            _strCurrentInvDt = CType(grdInvestors.DataSource, DataTable).Clone
            _strHistoryInvDt = CType(grdInvestors.DataSource, DataTable).Clone
            _strLockedInvDt = CType(grdInvestors.DataSource, DataTable).Clone
            _strreference = CType(grdInvestors.DataSource, DataTable).Clone
            _strDeletedRows = CType(grdInvestors.DataSource, DataTable).Clone
            _strTempDt = grdInvestors.DataSource
            objInv = New cAddModifyInvestor
            Dim _isInvestorLocked As Boolean = False
            For i As Integer = 0 To _strTempDt.Rows.Count - 1
                _FundId = grdInvestors.Rows(i).Cells("InvestorID").Value.ToString() '+ ","
                result = cAddModifyInvestor.CanInvestorDelete(_FundId)
                If result <> "0" Then
                    cAddModifyInvestor.UpdateInvestorStatusToDelete(_FundId)
                    _strCurrentInvDt.ImportRow(_strTempDt.Rows(i))
                Else
                    message = cAddModifyInvestor.Delete_Investor(_FundId)
                    If (message <> "Investor deleted successfully") Then
                        _strreference.ImportRow(_strTempDt.Rows(i))
                    Else
                        id += grdInvestors.Rows(i).Cells("investorid").Value.ToString() + ","
                        _strDeletedRows.ImportRow(_strTempDt.Rows(i))
                    End If
                End If
            Next
            If (id <> "") Then
                Dim dv As New DataView(temp)
                dv.RowFilter = "investorid not in( " & id & ")"
                temp = dv.ToTable
                grdInvestors.DataSource = temp
                HideGridColumns()
            End If

            If (_strCurrentInvDt.Rows.Count <> 0) Then
                For i As Integer = 0 To _strCurrentInvDt.Rows.Count - 1
                    For j As Integer = 0 To grdInvestors.Rows.Count - 1
                        If (grdInvestors.Rows(j).Cells("InvestorId").Value.ToString() = _strCurrentInvDt.Rows(i).Item("InvestorId").ToString) Then
                            grdInvestors.Rows(j).Selected = True
                            Exit For
                        End If
                    Next
                Next
                btnCancel.Enabled = False
                Me.Focus()
                stopresize = True
                lblmessage.Text = "The following investors have been marked as delete. Once all references to these investors in projects have been removed, they will be deleted automatically."
                'MessageBox.Show("Following Investor(s) are being used in a current project and therefore can not be deleted or marked inactive. Please try again later", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                If (id <> "") Then
                    RaiseEvent deleteInvestor(temp, False)
                    RaiseEvent deleteInvestor(_strDeletedRows, True)
                    'ref = New refreshscreen(AddressOf refreshMainScreen(_strCurrentInvDt, False))
                    'Me.BeginInvoke(ref)
                End If
                'Exit Sub
            End If
            If (_strreference.Rows.Count = 0 And _strHistoryInvDt.Rows.Count = 0 And _strCurrentInvDt.Rows.Count = 0 And _strLockedInvestorNames.Trim = "" And id.Trim <> "") Then
                ' RaiseEvent deleteInvestor(_strTempDt, True)
                'ref = New refreshscreen(AddressOf refreshMainScreen(_strTempDt, True))
                'Me.BeginInvoke(ref)
                'Application.DoEvents()
                If _strDeletedRows.Rows.Count > 0 Then
                    RaiseEvent deleteInvestor(_strDeletedRows, True)
                End If
                If temp.Rows.Count > 0 Then
                    RaiseEvent deleteInvestor(temp, False)
                End If
                MessageBox.Show("Selected investor(s) deleted successfully", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Close()
            Else
                If temp.Rows.Count > 0 Then
                    RaiseEvent deleteInvestor(temp, False)
                End If
                If (id <> "") Then
                    Me.Focus()
                    stopresize = True
                    If _strDeletedRows.Rows.Count > 0 Then
                        RaiseEvent deleteInvestor(_strDeletedRows, True)
                    End If
                    'If temp.Rows.Count > 0 Then
                    '    RaiseEvent deleteInvestor(temp, False)
                    'End If
                    'RaiseEvent deleteInvestor(_strTempDt, False)
                    'ref = New refreshscreen(AddressOf refreshMainScreen)
                    'Me.BeginInvoke(ref)
                    'Application.DoEvents()
                    MessageBox.Show("Selected investor(s) deleted successfully", m_strMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If

            'Me.Close()
        Catch ex As Exception
            stopresize = True
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        Finally
            'Me.Close()
            _strLockedInvDt = Nothing
            _strCurrentInvDt = Nothing
            _strHistoryInvDt = Nothing
            If (Not Me.IsDisposed) Then
                btnOk.Enabled = True
                Me.Focus()
            End If
            If (id <> "") Then

                stopresize = True
            End If
        End Try
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If Not btnCancel.Enabled Then
            Me.Close()
            Return
        End If
        btnOk.Enabled = False
        Dim delete As deleteinv = Nothing
        Try
            delete = New deleteinv(AddressOf yes)
            Me.BeginInvoke(delete)
        Catch ex As Exception
            stopresize = True
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub refreshMainScreen(ByVal dt As DataTable, ByVal isDeleted As Boolean)
        RaiseEvent deleteInvestor(dt, isDeleted)
    End Sub
    Private Sub DeleteInvestor_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'frmdelInv = Nothing
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "frmInvestorManagement" Then
                RemoveHandler deleteInvestor, AddressOf CType(Application.OpenForms(lmFomrs), frmInvestorManagement).raiseeventfordelete
            End If
        Next
        Me.Dispose()
    End Sub

    Private Sub DeleteInvestor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'frmdelInv = New DeleteInvestor(Nothing)
        grdInvestors.DataSource = Datatable
        HideGridColumns()
        FormatGrid(grdInvestors)
        btnCancel.Focus()
        Me.grdInvestors.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect

    End Sub

    Public Sub HideGridColumns()
        Try
            With grdInvestors

                If .DisplayLayout.Bands(0).Columns.Exists("investorid") Then
                    .DisplayLayout.Bands(0).Columns("investorid").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Parent") Then
                    .DisplayLayout.Bands(0).Columns("Parent").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Ultimate Parent") Then
                    .DisplayLayout.Bands(0).Columns("Ultimate Parent").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Master Type") Then
                    .DisplayLayout.Bands(0).Columns("Master Type").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Activist") Then
                    .DisplayLayout.Bands(0).Columns("Activist").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("EquityAssets") Then
                    .DisplayLayout.Bands(0).Columns("EquityAssets").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Last Report Date") Then
                    .DisplayLayout.Bands(0).Columns("Last Report Date").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Equity Assets") Then
                    .DisplayLayout.Bands(0).Columns("Equity Assets").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Street1") Then
                    .DisplayLayout.Bands(0).Columns("Street1").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Street2") Then
                    .DisplayLayout.Bands(0).Columns("Street2").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("State") Then
                    .DisplayLayout.Bands(0).Columns("State").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Region") Then
                    .DisplayLayout.Bands(0).Columns("Region").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Mobile") Then
                    .DisplayLayout.Bands(0).Columns("Mobile").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Telephone") Then
                    .DisplayLayout.Bands(0).Columns("Telephone").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Extension") Then
                    .DisplayLayout.Bands(0).Columns("Extension").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Fax") Then
                    .DisplayLayout.Bands(0).Columns("Fax").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Fax 2") Then
                    .DisplayLayout.Bands(0).Columns("Fax 2").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Telephone") Then
                    .DisplayLayout.Bands(0).Columns("Telephone").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Mobile") Then
                    .DisplayLayout.Bands(0).Columns("Mobile").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Email") Then
                    .DisplayLayout.Bands(0).Columns("Email").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("IsFormOpend") Then
                    .DisplayLayout.Bands(0).Columns("IsFormOpend").Hidden = True
                End If
                If .DisplayLayout.Bands(0).Columns.Exists("Website") Then
                    .DisplayLayout.Bands(0).Columns("Website").Hidden = True
                End If
            End With
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    ''' <summary>
    ''' close the window
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Me.Close()
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        End Try
    End Sub

    Private Sub DeleteInvestor_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
        Me.Focus()
    End Sub

    Private Sub DeleteInvestor_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "DeleteInvestor" Then
                If (Me.WindowState = FormWindowState.Normal) Then
                    CType(Application.OpenForms(lmFomrs), DeleteInvestor).WindowState = Me.WindowState
                End If
                If (Me.WindowState = FormWindowState.Maximized) Then
                    CType(Application.OpenForms(lmFomrs), DeleteInvestor).WindowState = Me.WindowState
                End If
                If (Me.WindowState = FormWindowState.Minimized) Then
                    If (stopresize = True) Then Exit Sub
                    Me.WindowState = FormWindowState.Normal
                    Application.DoEvents()
                End If
            End If
        Next


    End Sub

    Private Sub grdInvestors_BeforeEnterEditMode(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdInvestors.BeforeEnterEditMode
        'Me.grdInvestors.ActiveCell.Column.CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit
        grdInvestors.ActiveRow.Activation = Activation.ActivateOnly
    End Sub

    Private Sub grdInvestors_InitializeLayout(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdInvestors.InitializeLayout
        e.Layout.Override.SelectTypeRow = Infragistics.Win.UltraWinGrid.SelectType.None
    End Sub

#End Region

End Class